/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 6:48.
 */
package com.hyyt.shopjsp.promotion.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.promotion.service.IPlatformPromotionService;
import com.hyyt.shopjsp.promotion.service.IShopSalesPromotionDiscountService;
import com.hyyt.shopjsp.promotion.pojo.PlatformPromotion;
import com.hyyt.shopjsp.promotion.pojo.ShopSalesPromotionDiscount;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator on 2017/10/24 0024.
 */
@Controller
@RequestMapping("/back/promotion")
public class PlatformPromotionController extends BaseController {

    private Logger log = Logger.getLogger(PlatformPromotionController.class);
    private String basePath = "/back/shop/promotion/";

    @Resource
    private IPlatformPromotionService platformPromotionService;
    @Resource
    private IShopSalesPromotionDiscountService shopSalesPromotionDiscountService;


    /**
     * 进入平台管理促销活动列表页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoPromotionPage")
    public String gotoPromotionPage() {
        return basePath + "platformPromotionList";
    }

    /**
     * 查询平台管理促销活动集合
     * @param beginTime 活动开启时间
     * @param endTime 活动结束时间
     * @param isPass 审核状态
     * @param useStatus 启用状态
     * @param response 结果集
     */
    @RequestMapping(value = "listPlatformPromotion", method = RequestMethod.POST)
    @ResponseBody
    public void listPlatformPromotion(String beginTime,String endTime,String isPass,String useStatus,Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(beginTime)) {//是否检索活动开启时间
            param.put("beginTime", beginTime);
        }
        if (Utils.objectIsNotEmpty(endTime)) {//是否检索活动结束时间
            param.put("endTime", endTime);
        }
        if (Utils.objectIsNotEmpty(isPass)) {//是否检索审核状态
            param.put("isPass", isPass);
        }
        if (Utils.objectIsNotEmpty(useStatus)) {//是否检索启用状态
            param.put("useStatus", useStatus);
        }
        param.put("sortColumns", "promotionId desc");

        int totalRecordCount = platformPromotionService.countPlatformPromotion(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<PlatformPromotion> list = platformPromotionService.queryPlatformPromotion(param, page.getPageRecordBeginIndex(), page.getPageSize());

        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list

        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据id获得促销活动对象
     * @param promotionId 促销活动ID
     * @return 返回促销活动对象
     */
    @RequestMapping(value = "getPlatformPromotionObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getPlatformPromotionObject(Integer promotionId) {
        if (Utils.objectIsNotEmpty(promotionId)) {
            PlatformPromotion platformPromotion = platformPromotionService.loadPlatformPromotion(promotionId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(platformPromotion));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 保存或者修改促销活动信息
     *
     * @param platformPromotion 促销活动对象
     * @return 返回促销活动对象
     */
    @RequestMapping(value = "saveOrUpdatePlatformPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdatePlatformPromotion(PlatformPromotion platformPromotion) {
        if (Utils.objectIsNotEmpty(platformPromotion)) {
            platformPromotion.setIsPass(1);
            platformPromotion.setUseStatus(0);
            if (Utils.objectIsEmpty(platformPromotion.getPromotionId())) {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String createTimeStr = sdf.format(date);
                Double random = Math.random();
                String num = random.toString().substring(2, 8);
                String promotionNumber = createTimeStr + num;
                platformPromotion.setPromotionNumber(promotionNumber);
                platformPromotion.setCreateTime(new Timestamp(System.currentTimeMillis()));
                platformPromotion.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                platformPromotionService.savePlatformPromotion(platformPromotion);
            } else {
                platformPromotion.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                platformPromotionService.updatePlatformPromotion(platformPromotion);
            }
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id删除促销活动
     *
     * @param promotionId 促销活动ID
     */
    @RequestMapping(value = "deletePlatformPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deletePlatformPromotion(Integer promotionId) {
        if (Utils.objectIsNotEmpty(promotionId)) {
            platformPromotionService.deletePlatformPromotion(promotionId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 审核促销活动
     *
     * @param promotionId
     * @param isPass
     * @return
     */
    @RequestMapping(value = "auditPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg auditPromotion(Integer promotionId,String isPass) {
        boolean flag=false;
        if (Utils.objectIsNotEmpty(promotionId) && Utils.objectIsNotEmpty(isPass)) {
            PlatformPromotion platformPromotion = platformPromotionService.loadPlatformPromotion(promotionId);
            if(Utils.objectIsNotEmpty(platformPromotion)&&Utils.objectIsNotEmpty(platformPromotion.getPromotionId())){
                platformPromotion.setIsPass(Integer.valueOf(isPass));//审核状态
                Timestamp date =new Timestamp(System.currentTimeMillis());//获取当前系统timestamp
                platformPromotion.setCheckTime(date);
                BasicUsers basicUsers = getSessionUsers();
                platformPromotion.setVerifier(basicUsers.getUserName());
                platformPromotionService.updatePlatformPromotion(platformPromotion);
                flag=true;
            }
            return ResultMsg.create(flag);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 查询促销活动状态和审核状态
     *
     * @param promotionId 促销活动id
     * @return 返回状态值
     */
    @RequestMapping(value = "checkPromotionType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg checkPromotionType(Integer promotionId) {
        boolean flag=false;
        if (Utils.objectIsNotEmpty(promotionId)) {
            PlatformPromotion platformPromotion=platformPromotionService.loadPlatformPromotion(promotionId);
            if (platformPromotion.getUseStatus()==0&&platformPromotion.getIsPass()==2){
                flag=true;
            }
            return ResultMsg.create(flag);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 获取促销活动折扣
     * @param promotionId 促销活动id
     */
    @RequestMapping(value = "getSalesPromotionDiscountObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getSalesPromotionDiscountObject(Integer promotionId) {
        if (Utils.objectIsNotEmpty(promotionId)) {
            ShopSalesPromotionDiscount shopSalesPromotionDiscount=shopSalesPromotionDiscountService.loadShopSalesPromotionDiscount(promotionId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopSalesPromotionDiscount));
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 维护促销折扣
     *
     * @param shopSalesPromotionDiscount 促销活动折扣对象
     * @return 返回促销活动折扣对象
     */
    @RequestMapping(value = "saveOrUpdateSalesPromotionDiscount", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateSalesPromotionDiscount(ShopSalesPromotionDiscount shopSalesPromotionDiscount) {
        if (Utils.objectIsNotEmpty(shopSalesPromotionDiscount)) {
            PlatformPromotion platformPromotion=platformPromotionService.loadPlatformPromotion(shopSalesPromotionDiscount.getPromotionId());
            shopSalesPromotionDiscount.setPromotionId(platformPromotion.getPromotionId());
            shopSalesPromotionDiscount.setPromotionIdNumber(platformPromotion.getPromotionNumber());
            if (Utils.objectIsEmpty(shopSalesPromotionDiscount.getDisproductId())){
                shopSalesPromotionDiscountService.saveShopSalesPromotionDiscount(shopSalesPromotionDiscount);
            }else{
                shopSalesPromotionDiscountService.updateShopSalesPromotionDiscount(shopSalesPromotionDiscount);
            }
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 开启活动
     *
     */
    @RequestMapping(value = "openPlatformPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg openPlatformPromotion(Integer promotionId) {
        if (Utils.objectIsNotEmpty(promotionId)) {
            PlatformPromotion platformPromotion=platformPromotionService.loadPlatformPromotion(promotionId);
            if (platformPromotion!=null&&platformPromotion.getUseStatus()==0&&platformPromotion.getIsPass()==2) {
                platformPromotion.setUseStatus(1);
                platformPromotionService.updatePlatformPromotion(platformPromotion);
                return ResultMsg.create(true);
            }else if (platformPromotion!=null&&platformPromotion.getUseStatus()==2&&platformPromotion.getIsPass()==2){
                platformPromotion.setUseStatus(1);
                platformPromotionService.updatePlatformPromotion(platformPromotion);
                return ResultMsg.create(true);
            }else if (platformPromotion!=null&&platformPromotion.getUseStatus()==1&&platformPromotion.getIsPass()==2){
                return ResultMsg.create(false,"hasOpen");
            }else{
                return ResultMsg.create(false);
            }
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 关闭活动
     *
     */
    @RequestMapping(value = "closePlatformPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg closePlatformPromotion(Integer promotionId) {
        if (Utils.objectIsNotEmpty(promotionId)) {
            PlatformPromotion platformPromotion=platformPromotionService.loadPlatformPromotion(promotionId);
            if(platformPromotion!=null&&platformPromotion.getIsPass()==2&&platformPromotion.getUseStatus()==1){
                platformPromotion.setUseStatus(2);
                platformPromotionService.updatePlatformPromotion(platformPromotion);
                return ResultMsg.create(true);
            }else{
                return ResultMsg.create(false);
            }
        }else {
            return ResultMsg.create(false);
        }
    }
}
